﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeaBattle.Domain.Entities
{
    public class Fleet
    {
        private Dictionary<Ship,ShipState> ships = new Dictionary<Ship,ShipState>();

        //public Fleet(byte maxShipCount, byte maxPartCount)
        //{
        //    this.MaxShipCount = maxShipCount;
        //    this.MaxPartCount = maxPartCount;
        //}

        public int Id { get; set; }

        //private bool CanAddShip(Ship ship)
        //{
        //    if (ships.Count + 1 > MaxShipCount)
        //        return false;
        //    int Count=0;
        //    foreach (KeyValuePair<Ship,ShipState> x in ships)
        //        Count+=x.Key.PartsCount;
        //    if (Count + ship.PartsCount > MaxPartCount)
        //     return false;
        //    return true;
        //}

        public void AddShip(Ship ship,ShipState ShipState)
        {
            //if (CanAddShip(ship))
            //{
                ships.Add(ship,ShipState);
            //}
        }

        public void RemoveShip(Ship ship)
        {
            ships.Remove(ship);
        }

        public void Clear()
        {
            ships.Clear();
        }

        public ShotResult RemoveShipPart(int X, int Y)
        {
            foreach (var ship in ships.Keys)
            {
                ShipPart Part = ship.Parts.FirstOrDefault(x => (x.XCoord == X) && (x.YCoord == Y) && (x.Alive==true));
                if (Part != null)
                {
                    Part.ReceiveShot(X, Y);
                    if (ship.Parts.FirstOrDefault(x => x.Alive == true) != null)
                        return ShotResult.ResultWounded;
                    else
                        return ShotResult.ResultKilled;
                }                
            }
            return ShotResult.ResultMissed;
        }

        public IEnumerable<Ship> GetShips()
        {
            return ships.Keys.ToList();
        }
    }
}
